** Clearing Stata memory
capture log close
clear all
set more off, perm
set seed 1234

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////// Table O.5: Descriptive Statistics with Averages and Standard Deviations of Normalized Scores ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

*********************************************************************************
**************** Main sample ****************************************************
*********************************************************************************

* 1) Only years before the affirmative action took place
drop if aa_year==1
drop if year==2000
tab year


****************************************************************************************
*** Table - Descriptive statistics with averages and standard deviations of scores *****
****************************************************************************************

** Before excluding Portuguese and Foreign Language scores

** Panel A: Phase 1 scores

drop norm_p1score_g

foreach x in "norm_" {

gen `x'Essay=`x'p1score if Portuguese==1
label var `x'Essay "Essay"

foreach v of varlist Biology - Physics {
gen `x'p1score_`v'=`x'p1score if `v'==1
label var `x'p1score_`v' "`v'"
}
sum `x'p1score_Language
drop `x'p1score_Language

estimates clear

* All subjects
estpost sum `x'p1score_* `x'Essay if female==1 
matrix sdfemale=e(sd)
matrix list sdfemale
estpost sum `x'p1score_* `x'Essay if female==0 
matrix sdmale=e(sd)
matrix list sdmale
estpost ttest `x'p1score_* `x'Essay, by(female)
eststo all_subjects
estadd matrix sdfemale: all_subjects
estadd matrix sdmale: all_subjects

* Priority subjects
estpost sum `x'p1score_* `x'Essay if female==1  & priority==1
matrix sdfemale=e(sd)
matrix list sdfemale
estpost sum `x'p1score_* `x'Essay if female==0 & priority==1
matrix sdmale=e(sd)
matrix list sdmale
estpost ttest `x'p1score_* `x'Essay if priority==1 , by(female)
eststo priority_subjects
estadd matrix sdfemale: priority_subjects
estadd matrix sdmale: priority_subjects

* Non priority subjects
estpost sum `x'p1score_* `x'Essay if female==1  & priority==0
matrix sdfemale=e(sd)
matrix list sdfemale
estpost sum `x'p1score_* `x'Essay if female==0 & priority==0
matrix sdmale=e(sd)
matrix list sdmale
estpost ttest `x'p1score_* `x'Essay if priority==0 , by(female)
eststo non_priority_subjects
estadd matrix sdfemale: non_priority_subjects
estadd matrix sdmale: non_priority_subjects

esttab all_subjects  priority_subjects non_priority_subjects using "Output/descriptive_`x'scores.tex", cells( (mu_1(fmt(2)) mu_2(fmt(2)) b(star fmt(2))) (sdmale(par fmt(2)) sdfemale( par fmt(2)))  (N_1(fmt(%9.0fc)) N_2(fmt(%9.0fc)))) ///
refcat(`x'p1score_Biology "\textbf{Panel A: Phase 1 scores}", nolabel) booktabs nonumbers nomtitle noobs nogap  replace ///
collabels ("Men" "Women" "Diff." "Men" "Women" "Diff." "Men" "Women" "Diff.") star(* 0.10 ** 0.05 *** 0.01)   ///
 mgroups("All subjects" "Priority subjects" "Non priority subjects", pattern(1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))    label f 

}

** Panel B: Phase 2 scores
foreach x in "norm_" {
tab subject, sum(`x'score)
label var `x'score "All subjects"

foreach v of varlist Biology - Portuguese {
gen `x'p2score_`v'=`x'score if `v'==1
label var `x'p2score_`v' "`v'"
}
estimates clear

* All subjects
estpost sum `x'p2score_* if female==1 
matrix sdfemale=e(sd)
matrix list sdfemale
estpost sum `x'p2score_* if female==0 
matrix sdmale=e(sd)
matrix list sdmale
estpost ttest `x'p2score_*, by(female)
eststo all_subjects
estadd matrix sdfemale: all_subjects
estadd matrix sdmale: all_subjects

* Priority subjects
preserve
drop `x'p2score_Language
estpost sum `x'p2score_* if female==1  & priority==1
matrix sdfemale=e(sd)
matrix list sdfemale
estpost sum `x'p2score_* if female==0 & priority==1
matrix sdmale=e(sd)
matrix list sdmale
estpost ttest `x'p2score_* if priority==1, by(female)
eststo priority_subjects
estadd matrix sdfemale: priority_subjects
estadd matrix sdmale: priority_subjects
restore

* Non priority subjects
estpost sum `x'p2score_* if female==1  & priority==0
matrix sdfemale=e(sd)
matrix list sdfemale
estpost sum `x'p2score_* if female==0 & priority==0
matrix sdmale=e(sd)
matrix list sdmale
estpost ttest `x'p2score_* if priority==0 , by(female)
eststo non_priority_subjects
estadd matrix sdfemale: non_priority_subjects
estadd matrix sdmale: non_priority_subjects

esttab all_subjects  priority_subjects non_priority_subjects using "Output/descriptive_`x'scores.tex", cells( (mu_1(fmt(2)) mu_2(fmt(2)) b(star fmt(2))) (sdmale(par fmt(2)) sdfemale( par fmt(2)))  (N_1(fmt(%9.0fc)) N_2(fmt(%9.0fc)))) refcat(`x'p2score_Biology "\textbf{Panel B: Phase 2 scores}", nolabel) booktabs nonumbers nomtitle noobs nogap  append ///
star(* 0.10 ** 0.05 *** 0.01)  label f collabels(none)

}